from common.lib import Singleton
from logger.logger import Logger
import gtk
from gtk import gdk
import cairo

import common.gtkobjects.images as images
import common.gtkobjects.gtkicon as gtkicon

logger = Logger.get_instance()

class DeskletWindow(gtk.Window):
    '''Main Desklet - a transparent gtkWindow that 
    will be placed on the desktop
    '''
    supports_alpha = False
    
    def hide_callback(self, widget=None, data=None):
        widget.hide()
    
    def clicked_callback(self, widget=None, data=None):
        widget.set_decorated(not widget.get_decorated())
    
    def expose_callback(self, widget=None, data=None):
        global supports_alpha
        
        cr = widget.window.cairo_create()
        if supports_alpha:
            cr.set_source_rgba(1.0,1.0,1.0,0.0) #transparent
        else:
            cr.set_source_rgb(1.0, 1.0, 1.0) # Opaque white
            
        #background
        cr.set_operator(cairo.OPERATOR_SOURCE)
        cr.paint()
        
        #FIXME: this is a circle, change later
    #       (width, height) = self.get_size()
    #       cr.set_source_rgba(1.0, 0.2, 0.2, 0.6)
    #      if width < height:
    #         radius = float(width)/2 - 0.8
    #    else:
    #       radius = float(height)/2 - 0.8
    
        #shape
        x0 = 25.6
        y0 = 25.6
        rect_width = 204.8
        rect_height = 204.8
        radius = 102.4
        
        x1 = x0 + rect_width
        y1 = y0 + rect_height
        if rect_width/2 < radius:
            if rect_height/2 < radius:
                cr.move_to(x0, (y0 + y1)/2)
                cr.curve_to(x0, y0, x0, y0, (x0+x1)/2, y0)
                cr.curve_to(x1, y0, x1, y0, x1, (y0 + y1)/2)
                cr.curve_to(x1, y1, x1, y1, (x1 + x0)/2, y1)
                cr.curve_to(x0, y1, x0, y1, x0, (y0 + y1)/2) 
            else:
                cr.move_to(x0, y0 + radius)
                cr.curve_to(x0 ,y0, x0, y0, (x0 + x1)/2, y0)
                cr.curve_to(x1, y0, x1, y0, x1, y0 + radius)
                cr.line_to(x1 , y1 - radius)
                cr.curve_to(x1, y1, x1, y1, (x1 + x0)/2, y1)
                cr.curve_to(x0, y1, x0, y1, x0, y1- radius)
        else:
            if rect_height/2 < radius:
                cr.move_to(x0, (y0 + y1)/2)
                cr.curve_to(x0 , y0, x0 , y0, x0 + radius, y0)
                cr.line_to(x1 - radius, y0)
                cr.curve_to(x1, y0, x1, y0, x1, (y0 + y1)/2)
                cr.curve_to(x1, y1, x1, y1, x1 - radius, y1)
                cr.line_to(x0 + radius, y1)
                cr.curve_to(x0, y1, x0, y1, x0, (y0 + y1)/2)
            else:
                cr.move_to(x0, y0 + radius)
                cr.curve_to(x0 , y0, x0 , y0, x0 + radius, y0)
                cr.line_to(x1 - radius, y0)
                cr.curve_to(x1, y0, x1, y0, x1, y0 + radius)
                cr.line_to(x1 , y1 - radius)
                cr.curve_to(x1, y1, x1, y1, x1 - radius, y1)
                cr.line_to(x0 + radius, y1)
                cr.curve_to(x0, y1, x0, y1, x0, y1- radius)    
        
        cr.set_source_rgba(1.0, 0.2, 0.2, 0.6)
        cr.paint_with_alpha(0.5)  
        cr.fill()
        cr.stroke()
        return False
        
        
    def screen_changed(self, widget=None, data=None):
        global supports_alpha
        
        screen = self.get_screen()
        colormap = screen.get_rgba_colormap()
        if colormap == None:
            logger.error('This screen does not support alpha channels!')
            colormap = screen.get_rgb_colormap()
            supports_alpha = False
        else:
            logger.DEBUG('This screen supports alpha channels!')
            supports_alpha = True
            
        self.set_colormap(colormap)
        return False
    
    def __init__(self):
        
        gtk.Window.__init__(self)
        self.set_keep_below(True)
        self.connect('delete-event', self.hide_callback)
        self.connect('screen-changed', self.screen_changed)
        self.add_events(gdk.BUTTON_PRESS_MASK)
        self.connect('button-press-event', self.clicked_callback)
        self.connect('expose-event', self.expose_callback)
        self.set_app_paintable(True)
        self.set_decorated(False)
        
        self.screen_changed(widget=self)
        
        

class PreferencesWindow(gtk.Window, Singleton):
    ''' A singleton class that implements the Preferences Window'''   
    def __init__(self):
        gtk.Window.__init__(self)
        self.set_title('Preferences')
